package com.happypets.backstage;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.happypets.servicesImpl.PetServiceImpl;

public class AllPages extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public AllPages() {
        super();
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json;charset=UTF-8");
		response.setHeader("Cache-Control", "no-store"); 
		
		PetServiceImpl psi = new PetServiceImpl();
		
//		HttpSession session = request.getSession();
		
		PrintWriter out = response.getWriter();
		
		List<Map<String,String>> lm = new ArrayList<Map<String,String>>();
		
		Map<String,String> m = new HashMap<String,String>();
		
		String flag = request.getParameter("flag");
		
		int pageShowNumber = 5;
		
		/**
		 * pet 1
		 * user 2
		 * manager 3
		 * form 4
		 */
		
		int pages = 0;
		
		switch(flag){
		case "pet": 
			pages = psi.AllPages(1, pageShowNumber);
			break;
			
		case "user": 
			pages = psi.AllPages(2, pageShowNumber);
			break;
			
		case "manager": 
			pages = psi.AllPages(3, pageShowNumber);
			break;
			
		case "form": 
			pages = psi.AllPages(4, pageShowNumber);
			break;
		}
		
		m.put("pages", String.valueOf(pages));
		
		lm.add(m);
		
		
		Gson gson=new Gson();
		String json=gson.toJson(lm);
		System.out.println(json);
		out.write(json);
		
		out.flush();
		out.close();
		
		
		
		
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
